package com.lc.zpyh.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lc.zpyh.R;
import com.lc.zpyh.http.response.SelectOneLVBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.home.InviteFriendsActivity;
import com.lc.zpyh.ui.activity.home.SelectMerchantsFlActivity;
import com.lc.zpyh.ui.activity.mine.LoginCodeActivity;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.CourseUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;
import com.lc.zpyh.util.eventbus.EventBusUtils;
import com.lc.zpyh.widget.UIHelper;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BaoJievp2Adapter  extends RecyclerView.Adapter<BaoJievp2Adapter.ViewHolder> {
    public Context context;
   private  List<SelectOneLVBean.MerchantClassifiesBean> dataList;
    public BaoJievp2Adapter(Context context) {
        this.context = context;
    }

    public void setDataList(List<SelectOneLVBean.MerchantClassifiesBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vp2_view_baojie, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        List<SelectOneLVBean.MerchantClassifiesBean> jigouList = new ArrayList<>();
        if(position == 0){
            if (dataList.size()>=8) {
                for (int i = 0; i < 8; i++) {
                    jigouList.add(dataList.get(i));
                }
            }else {
                for (int i = 0; i < dataList.size(); i++) {
                    jigouList.add(dataList.get(i));
                }
            }
        }else if (position==1){
            if (dataList.size()>=16) {
                for (int i = 0; i < 8; i++) {
                    jigouList.add(dataList.get(8 + i));

                }
            }else {
                for (int i = 0; i <dataList.size()- 8; i++) {
                    jigouList.add(dataList.get(8 + i));

                }
            }
        }else {
            if (dataList.size()>=24) {
                for (int i = 0; i < 8; i++) {
                    jigouList.add(dataList.get(16 + i));

                }
            }else {
                for (int i = 0; i <dataList.size()- 16; i++) {
                    jigouList.add(dataList.get(16 + i));

                }
            }
        }


        GridLayoutManager lm = new GridLayoutManager(context, 4);

        lm.setOrientation(GridLayoutManager.VERTICAL);

        holder.rvNeijigou.setLayoutManager(lm);
        NeijigouRecyAdapter   neijigouRecyAdapter = new NeijigouRecyAdapter(context, jigouList);
        holder.rvNeijigou.setAdapter(neijigouRecyAdapter);
        neijigouRecyAdapter.setOnChildClickListener(new NeijigouRecyAdapter.onChildClickListener() {
            @Override
            public void onChildClick(RecyclerView parent, View view, int position, SelectOneLVBean.MerchantClassifiesBean data) {
                if (CourseUtils.isNotNullOrEmpty(SPUtil.get(context, IntentKey.USERID, ""))) {
                    if (data.getClassifyName().equals("邀请好友")) {
                        UIHelper.startActivity(context, InviteFriendsActivity.class);
                    } else if (data.getClassifyName().equals("优惠券")) {
                        EventBusUtils.sendEvent(new Event(EvenConstant.COUPON));
                    } else
                        context.startActivity(new Intent(context, SelectMerchantsFlActivity.class).putExtra("canteenId",data.getCanteenId()+"").putExtra("flid", data.getMerchantClassifyId() + "").putExtra("schoolid", data.getSchoolId() + "").putExtra("title", data.getClassifyName()));
                    Log.e("TAG", "onChildClick: "+data.getCanteenId()+"" );
//
                }else {
                    context.startActivity(new Intent(context,LoginCodeActivity.class));
                    return;
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return (int) Math.ceil(Double.parseDouble(BigDecimalUtils.div( dataList.size()+"",8+"",2)+""));
    }


    public void bindView(ViewHolder holder, int position) {

    }



    static class ViewHolder extends RecyclerView.ViewHolder {
//
        @BindView(R.id.rv_jigou)
        RecyclerView rvNeijigou;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

